#ifndef EDITOR_AREA_H
#define EDITOR_AREA_H

#include <idocument.h>
#include <ieditor.h>
#include <ieditorarea.h>

#include <QtCore/QHash>
#include <QtCore/QList>
#include <QtGui/QTabWidget>

namespace core
{
class IDocument;
class IEditor;
}

namespace gui
{

class EditorArea : public QTabWidget, public core::IEditorArea
{
    Q_OBJECT

public:
    EditorArea(QWidget* parent = 0);
    ~EditorArea();

    // IEditorArea
    uint openedEditorsCount() const;

    QList<QSharedPointer<core::IEditor> > openedEditors(
        const QSharedPointer<core::IDocument>& document) const;

    QSharedPointer<core::IEditor> currentEditor() const;

    bool event(QEvent* event);

private:
    void createActions();
    void connectSlots();

    bool childrenHasFocus() const;

    int tabFor(const QSharedPointer<core::IEditor>& editor) const;

signals:
    void editorOpened(const QSharedPointer<core::IEditor> editor);
    void aboutToSave(const QSharedPointer<core::IDocument> document);
    void aboutToClose(const QSharedPointer<core::IEditor> editor);
    void currentChanged(const QSharedPointer<core::IEditor> editor);

public slots:
    // IEditorArea
    void openEditor(const QSharedPointer<core::IDocument>& document);
    void closeEditor(const QSharedPointer<core::IEditor>& editor);
    void confirmClose(const QSharedPointer<core::IEditor>& editor);

private slots:
    void editorChange(int index);
    void closeTab(int index);
    void ensureEditorNameConsistency(bool isDirty = false);

    void saveCurrentDocument();

private:
    QHash<QSharedPointer<core::IDocument>,
        QList<QSharedPointer<core::IEditor> > > m_openedEditors;

    QAction* m_saveCurrentDocumentAction;

    bool m_editorFocused;
};

} // namespace gui

#endif
